// const baseURL = 'http://47.93.61.25:8820/';
// const baseURL = 'http://47.106.140.77:8820/';
// const baseURL = 'http://yeecat.cn:8820/';
const baseURL = 'https://yeecat.cn/';
import {
  refreshToken
} from '~/services/api/login'


const CODE_MESSAGE = {
  200: '服务器成功返回请求数据',
  201: '新建或修改数据成功',
  202: '一个请求已经进入后台排队(异步任务)',
  204: '删除数据成功',
  400: '发出信息有误',
  401: '用户没有权限(令牌失效、用户名、密码错误、登录过期)',
  402: '令牌过期',
  403: '用户得到授权，但是访问是被禁止的',
  404: '访问资源不存在',
  406: '请求格式不可得',
  410: '请求资源被永久删除，且不会被看到',
  500: '服务器发生错误',
  502: '网关错误',
  503: '服务不可用，服务器暂时过载或维护',
  504: '网关超时',
};

const request = ({
  url,
  method,
  data,
  header
}) => {
  //  加载请求拦截器
  //  token
  const role = wx.getStorageSync('role');
  const token = role?.tokenId;
  const userId = role?.userId;
  console.log(token, userId);
  if (!header.token && token) {
    header.token = token;
    // header.userId = userId;
  }

  if (!header.userId && userId) {
    header.userId = userId;
  }


  wx.showLoading({
    title: '请稍等...',
    mask: true,
  });
  return new Promise((resolve, reject) => {
    wx.request({
      url: baseURL + url,
      method,
      header,
      data,
      timeout: 30000,
      // enableHttp2:true,
      success({
        data
      }) {
        wx.hideLoading();
        const {
          code
        } = data;
        console.log(code, 'code');
        switch (code) {

          case '200':
            //  请求成功
            resolve(data);
            break;
          case '401':
          case '101':
          case '103':
            wx.navigateTo({
              url: '/pages/login/login',
            });
            break;
          case '102':
            // 短token 失效 refreshToken
            refreshToken().then((res) => {
              if (res && res.data) {
                wx.setStorage({
                  key: 'role',
                  data: res.data
                });
              }
            })
            break;
          default:
            wx.showToast({
              title: data.msg,
              icon: 'none',
              duration: 2000,
            });
            return resolve(data);
        }
      },
      fail: function (err) {
        // wx.hideLoading();
        // wx.showToast({
        //   title: err.message,
        //   icon: 'error',
        //   duration: 2000,
        // });
        reject(err);
      },
      complete() {
        // wx.hideLoading();
      },
    });
  });
};

const _post = async ({
  url,
  data,
  header = {
    'content-type': 'application/json',
    // devType: 4,
    // appId: "crm_agent",
    // authType: "agent",
    appId: 'crm_agent',
    authType: "agent",
    devType: 4,
    // token: '1812e7d7e62c4bd08db9613b8c958b67',
    // userId: "726571921824329728",


    ...header
  },
}) => {
  console.log(header, 'header')
  const res = await request({
    url,
    data,
    header,
    method: 'POST',
  });
  return res;
};
const _put = async ({
  url,
  data,
  header = {
    'content-type': 'application/json',
    devType: 4,
    // appId: "crm_agent",
    // authType: "agent",
    appId: 'crm_agent',
    authType: "agent",
    ...header
  },
}) => {
  console.log(header, 'heee');
  const res = await request({
    url,
    data,
    header,
    method: 'PUT',
  });
  return res;
};
const _get = async ({
  url,
  data,
  header = {
    'content-type': 'application/json',
    devType: 4,
    // appId: "crm_agent",
    // authType: "agent"
    appId: 'crm_agent',
    authType: "agent",
  },
}) => {
  const res = await request({
    // 'content-type': 'application/x-www-form-urlencoded'
    url,
    data,
    header,
    method: 'GET',
  });
  return res;
};

module.exports = {
  baseURL,
  request,
  _post,
  _put,
  _get,
};